package com.adcc.ags.gms.gw.controller;

import com.adcc.ags.gms.core.util.Constant;
import com.adcc.ags.gms.core.util.LogFactory;
import com.adcc.ags.gms.core.vo.ResultState;
import com.adcc.ags.gms.core.vo.ResultVO;
import com.adcc.ags.gms.gw.service.GMSLogService;
import com.adcc.ags.gms.gw.vo.GWLogVO;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Optional;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping(value="/log",produces = MediaType.APPLICATION_JSON_VALUE)
public class GMSLogController {
    @Autowired
    private GMSLogService gmsLogService;

    @Autowired
    private LogFactory log;

    @RequestMapping(value = "/getLogsByCondition", method = RequestMethod.GET)
    public @ResponseBody ResultVO getLogsByCondition(String logLevel, String queryOperateName, String dateStart, String dateEnd, String queryKeyword, int currentPage, int pageSize){
        ResultVO resultVO = new ResultVO();
        try {
            Optional<PageInfo<GWLogVO>> optional = gmsLogService.findAllLogs(logLevel, queryOperateName, dateStart, dateEnd, queryKeyword, currentPage, pageSize);
            if(optional.isPresent()){
                resultVO.setPageInfo(optional.get());
                if(optional.get().getList().size() > 0){
                    List<GWLogVO> list = Lists.newArrayListWithExpectedSize(100);
                    for(GWLogVO gwLogVO : optional.get().getList()){
                        list.add(gwLogVO);
                    }
                    resultVO.getData().put("gwLogVOList",list);
                }
            } else {
                resultVO.setCode(-1);
                resultVO.setState(ResultState.FAILURE);
                resultVO.setReason(Constant.GMS_DB_ERROR);
            }
            return resultVO;
        } catch (Exception ex) {
            log.error(GMSLogController.class, "getLogsByCondition() error", ex);
            resultVO.setCode(-1);
            resultVO.setState(ResultState.FAILURE);
            resultVO.setReason(Constant.GMS_SERVER_ERROR);
            return resultVO;
        }
    }

}
